********************************************************************************
*Project:  	Swiss Trade During the Covid-19 Pandemic: An Early Appraisal	
*Authors:	KB, SL, VP, PW										
*This File: Figure 3
*Version: 	vFinal							
*Date: 		2020-09-01				
********************************************************************************

********************************************************************************
*1. Lockdownn
********************************************************************************

*X(Imports)-Y(EXPORTS) GRAPHS BY GOODS
use "${root}\workfiles\trade_2002_2020_M_analysis_wide.dta", replace

keep if SAMPLE==51
keep SAMPLE jahr monat datum export_chf_mia export_tonnen import_chf_mia import_tonnen waren_art_kat12

reshape wide export_chf_mia export_tonnen import_chf_mia import_tonnen, i(jahr monat) j(waren_art_kat12)

tsset datum

sum monat if jahr==2020
keep if monat<=6

collapse (sum) export* import*, by(jahr)

tsset jahr

foreach ht in import export {		
	foreach unit in chf_mia tonnen {
	    forvalues wa=1(1)12 {
			gen RY_`ht'_`unit'`wa'=l.`ht'_`unit'`wa'
			gen `ht'_DEV_`unit'`wa'=`ht'_`unit'`wa'-l.`ht'_`unit'`wa'
			gen `ht'_DEV100_`unit'`wa'=100*(`ht'_`unit'`wa'-l.`ht'_`unit'`wa')/(l.`ht'_`unit'`wa')
		}
	}
}

foreach ht in import export {	
	foreach unit in chf_mia tonnen {
	egen `ht'_CUM_`unit'=														///
	rowtotal(`ht'_`unit'1 `ht'_`unit'2 `ht'_`unit'3 `ht'_`unit'4 `ht'_`unit'5 `ht'_`unit'6 `ht'_`unit'7 `ht'_`unit'8 `ht'_`unit'9 `ht'_`unit'10 `ht'_`unit'11 `ht'_`unit'12)
	gen RY_`ht'_CUM_`unit'=l1.`ht'_CUM_`unit'
	}
}

keep if jahr==2020


reshape long RY_export_chf_mia	RY_import_chf_mia RY_export_tonnen RY_import_tonnen	///
export_chf_mia import_chf_mia export_tonnen import_tonnen 			///
export_DEV_chf_mia import_DEV_chf_mia export_DEV100_chf_mia import_DEV100_chf_mia ///
export_DEV_tonnen import_DEV_tonnen export_DEV100_tonnen import_DEV100_tonnen, i(jahr) j(waren_art_kat12)

foreach unit in chf_mia tonnen {
	gen export_CUMDEV_`unit'=100*(export_CUM_`unit'-RY_export_CUM_`unit')/RY_export_CUM_`unit'
	gen import_CUMDEV_`unit'=100*(import_CUM_`unit'-RY_import_CUM_`unit')/RY_import_CUM_`unit'
	gen total_`unit'=export_`unit'+import_`unit'
	bysort jahr: egen total_`unit'_mean=mean(total_`unit')
	gen total_`unit'_relative_size=total_`unit'/total_`unit'_mean
}

gen total_lab="Total"

gen kat12_lab=""
replace kat12_lab="Agriculture" if waren_art_kat12==1
replace kat12_lab="Energy" if waren_art_kat12==2
replace kat12_lab="Textiles" if waren_art_kat12==3
replace kat12_lab="Paper" if waren_art_kat12==4
replace kat12_lab="Leather" if waren_art_kat12==5
replace kat12_lab="Pharmaceuticals" if waren_art_kat12==6
replace kat12_lab="Stones/Earth" if waren_art_kat12==7
replace kat12_lab="Metals" if waren_art_kat12==8
replace kat12_lab="Machines" if waren_art_kat12==9
replace kat12_lab="Vehicles" if waren_art_kat12==10
replace kat12_lab="Prec., Watches, Jewellery" if waren_art_kat12==11
replace kat12_lab="Various" if waren_art_kat12==12

count
local obs=r(N)+1
di `obs'
set obs `obs'

replace waren_art_kat12=1000 if waren_art_kat12==.
replace kat12_lab="Scale: 25 bn CHF trade" if waren_art_kat12==1000
replace total_chf_mia=25 if waren_art_kat12==1000
sum total_chf_mia_mean
replace total_chf_mia_mean=r(mean) if waren_art_kat12==1000
replace total_chf_mia_relative_size=total_chf_mia/total_chf_mia_mean
replace total_chf_mia_relative_size=total_chf_mia/total_chf_mia_mean
replace export_DEV100_chf_mia=-33 if  waren_art_kat12==1000
replace import_DEV100_chf_mia=17.5 if waren_art_kat12==1000

graph twoway 																	///															///
(scatter export_DEV100_chf_mia import_DEV100_chf_mia [w=total_chf_mia_relative_size], ///
msymbol(Oh)  mcolor(gs2)) ///
(scatter export_DEV100_chf_mia import_DEV100_chf_mia if waren_art_kat12==1000, ///
msymbol(O) msize(8)  mcolor(none) mlcolor(none)  mlwidth(none) mfcolor(gs14)) ///
(scatter export_DEV100_chf_mia import_DEV100_chf_mia if inlist(waren_art_kat12, 1000), ///
mlabel(kat12_lab) mlabp(9) mlabgap(*5.1) mlabcolor(maroon) mlabsize(vsmall) msymbol(i)  mcolor(gs2)) ///
(scatter export_DEV100_chf_mia import_DEV100_chf_mia if inlist(waren_art_kat12,1), ///
mlabel(kat12_lab) mlabp(12) mlabgap(*3.5) mlabcolor(maroon) msize(small) msymbol(i)  mcolor(gs2)) ///
(scatter export_DEV100_chf_mia import_DEV100_chf_mia if inlist(waren_art_kat12,2,5), ///
mlabel(kat12_lab) mlabp(12) mlabgap(*3) mlabcolor(maroon) msize(small) msymbol(i)  mcolor(gs2)) ///
(scatter export_DEV100_chf_mia import_DEV100_chf_mia if inlist(waren_art_kat12,11), ///
mlabel(kat12_lab) mlabp(12) mlabgap(*6) mlabcolor(maroon) msize(small) msymbol(i)  mcolor(gs2)) ///
(scatter export_DEV100_chf_mia import_DEV100_chf_mia if inlist(waren_art_kat12,7), ///
mlabel(kat12_lab) mlabp(3)  mlabcolor(maroon) msize(small) msymbol(i)  mcolor(gs2)) ///
(scatter export_DEV100_chf_mia import_DEV100_chf_mia if inlist(waren_art_kat12,9), ///
mlabel(kat12_lab) mlabp(3) mlabgap(*6) mlabcolor(maroon) msize(small) msymbol(i)  mcolor(gs2)) ///
(scatter export_DEV100_chf_mia import_DEV100_chf_mia if inlist(waren_art_kat12,12), ///
mlabel(kat12_lab) mlabp(6) mlabgap(*1.5) mlabcolor(maroon) msize(small) msymbol(i)  mcolor(gs2)) ///
(scatter export_DEV100_chf_mia import_DEV100_chf_mia if inlist(waren_art_kat12,3), ///
mlabel(kat12_lab) mlabp(5) mlabgap(*2) mlabcolor(maroon) msize(small) msymbol(i)  mcolor(gs2)) ///
(scatter export_DEV100_chf_mia import_DEV100_chf_mia if inlist(waren_art_kat12,8,10), ///
mlabel(kat12_lab) mlabp(6) mlabgap(*3) mlabcolor(maroon) msize(small) msymbol(i)  mcolor(gs2)) ///
(scatter export_DEV100_chf_mia import_DEV100_chf_mia if inlist(waren_art_kat12,4), ///
mlabel(kat12_lab) mlabp(9) mlabgap(*2) mlabcolor(maroon) msize(small) msymbol(i)  mcolor(gs2)) ///
(scatter export_DEV100_chf_mia import_DEV100_chf_mia if inlist(waren_art_kat12,6), ///
mlabel(kat12_lab) mlabp(9) mlabgap(*11) mlabcolor(maroon) msize(small) msymbol(i)  mcolor(gs2)), ///
yscale(range (-35 15)) xscale(range (-45 20)) ylabel(-30(10)10,grid) xlabel(-50(10)20,grid) ///
ytitle("%-Change in Cumulative Exports 2020/2019") ///
xtitle("%-Change in Cumulative Imports 2020/2019", margin(small)) ///
yline(0, lcolor(gs10) lwidth(medthick)) xline(0, lcolor(gs10) lwidth(medthick)) ///
scheme(s1color) legend(off)
graph export "${root}\figures\FIG3b_Yexport_Ximport_KAT12_COVID19_Percent.png", replace
graph export "${root}\figures\FIG3b_Yexport_Ximport_KAT12_COVID19_Percent.pdf", replace


********************************************************************************
********************************************************************************
********************************************************************************



*X(Imports)-Y(EXPORTS) GRAPHS BY Country
*CHECK STATA LIST ENTRY ON THE SIZE OF MARKERS: 
*www.statalist.org/forums/forum/general-stata-discussion/general/1538359-scatterplots-with-weighted-marker-size-revisited
use "${root}\workfiles\trade_2002_2020_M_analysis_wide.dta", replace

keep if SAMPLE==21

gen main=0
replace main=1 if inlist(land_id, 111, 112, 113, 114, 121, 122, 145, 373, 387, 403)
replace land_id=999 if main==0
drop main
collapse (sum) export* import_*, by(jahr monat land_id datum)

keep jahr monat datum export_chf_mia export_tonnen import_chf_mia import_tonnen land_id
reshape wide export_chf_mia export_tonnen import_chf_mia import_tonnen, i(jahr monat) j(land_id)

tsset datum

sum monat if jahr==2020
keep if monat<=6

collapse (sum) export* import*, by(jahr)

tsset jahr

foreach ht in import export {		
	foreach unit in chf_mia tonnen {
	    foreach la in  111 112 113 114 121 122 145 373 387 403 999{
			gen RY_`ht'_`unit'`la'=l.`ht'_`unit'`la'
			gen `ht'_DEV_`unit'`la'=`ht'_`unit'`la'-l.`ht'_`unit'`la'
			gen `ht'_DEV100_`unit'`la'=100*(`ht'_`unit'`la'-l.`ht'_`unit'`la')/(l.`ht'_`unit'`la')
		}
	}
}

foreach ht in import export {	
	foreach unit in chf_mia tonnen {
	egen `ht'_CUM_`unit'=														///
	rowtotal(`ht'_`unit'111 `ht'_`unit'112 `ht'_`unit'113 `ht'_`unit'114 `ht'_`unit'121 `ht'_`unit'122 `ht'_`unit'145 `ht'_`unit'373 `ht'_`unit'387 `ht'_`unit'403 `ht'_`unit'999)
	gen RY_`ht'_CUM_`unit'=l1.`ht'_CUM_`unit'
	}
}

keep if jahr==2020

reshape long RY_export_chf_mia	RY_import_chf_mia RY_export_tonnen RY_import_tonnen	///
export_chf_mia import_chf_mia export_tonnen import_tonnen 			///
export_DEV_chf_mia import_DEV_chf_mia export_DEV100_chf_mia import_DEV100_chf_mia ///
export_DEV_tonnen import_DEV_tonnen export_DEV100_tonnen import_DEV100_tonnen, i(jahr) j(land_id)

foreach unit in chf_mia tonnen {
	gen export_CUMDEV_`unit'=100*(export_CUM_`unit'-RY_export_CUM_`unit')/RY_export_CUM_`unit'
	gen import_CUMDEV_`unit'=100*(import_CUM_`unit'-RY_import_CUM_`unit')/RY_import_CUM_`unit'
	gen total_`unit'=export_`unit'+import_`unit'
	bysort jahr: egen total_`unit'_mean=mean(total_`unit')
	gen total_`unit'_relative_size=total_`unit'/total_`unit'_mean
}

gen land_str="Germany" if land_id==111
replace land_str="France" if land_id==112
replace land_str="Italy" if land_id==113
replace land_str="Netherlands" if land_id==114
replace land_str="Austria" if land_id==121
replace land_str="United Kingdom" if land_id==122
replace land_str="Spain" if land_id==145
replace land_str="China" if land_id==373
replace land_str="Japan" if land_id==387
replace land_str="United States" if land_id==403
replace land_str="Other Countries" if land_id==999

gen total_lab="Total"

count
local obs=r(N)+1
di `obs'
set obs `obs'

replace land_id=1000 if land_id==.
replace land_str="Scale: 25 bn CHF trade" if land_id==1000
replace total_chf_mia=25 if land_id==1000
sum total_chf_mia_mean
replace total_chf_mia_mean=r(mean) if land_id==1000
replace total_chf_mia_relative_size=total_chf_mia/total_chf_mia_mean
replace total_chf_mia_relative_size=total_chf_mia/total_chf_mia_mean
replace export_DEV100_chf_mia=-33 if  land_id==1000
replace import_DEV100_chf_mia=18 if land_id==1000

graph twoway 																	///
(scatter export_DEV100_chf_mia import_DEV100_chf_mia [w=total_chf_mia_relative_size], ///
msymbol(Oh)  mcolor(gs2)) ///
(scatter export_DEV100_chf_mia import_DEV100_chf_mia if land_id==1000, ///
msymbol(O) msize(5)  mcolor(none) mlcolor(none)  mlwidth(none) mfcolor(gs14)) ///
(scatter export_DEV100_chf_mia import_DEV100_chf_mia if inlist(land_id, 1000), ///
mlabel(land_str) mlabp(9) mlabgap(*3.2) mlabcolor(maroon) mlabsize(vsmall) msymbol(i)  mcolor(gs2)) ///
(scatter export_DEV100_chf_mia import_DEV100_chf_mia if inlist(land_id, 112, 122, 145), ///
mlabel(land_str) mlabp(12) mlabgap(*3) mlabcolor(maroon) msize(small) msymbol(i)  mcolor(gs2)) ///
(scatter export_DEV100_chf_mia import_DEV100_chf_mia if inlist(land_id, 403), ///
mlabel(land_str) mlabp(1) mlabgap(*3) mlabcolor(maroon) msize(small) msymbol(i)  mcolor(gs2)) ///
(scatter export_DEV100_chf_mia import_DEV100_chf_mia if inlist(land_id, 111), ///
mlabel(land_str) mlabp(3) mlabgap(*5) mlabcolor(maroon) msize(small) msymbol(i)  mcolor(gs2)) ///
(scatter export_DEV100_chf_mia import_DEV100_chf_mia if inlist(land_id, 114), ///
mlabel(land_str) mlabp(9) mlabgap(*2) mlabcolor(maroon) msize(small) msymbol(i)  mcolor(gs2)) ///
(scatter export_DEV100_chf_mia import_DEV100_chf_mia if inlist(land_id,387), ///
mlabel(land_str) mlabp(9) mlabgap(*2) mlabcolor(maroon) msize(small) msymbol(i)  mcolor(gs2)) ///
(scatter export_DEV100_chf_mia import_DEV100_chf_mia if inlist(land_id,999), ///
mlabel(land_str) mlabp(9) mlabgap(*5) mlabcolor(maroon) msize(small) msymbol(i)  mcolor(gs2)) ///
(scatter export_DEV100_chf_mia import_DEV100_chf_mia if inlist(land_id, 113, 121, 373), ///
mlabel(land_str) mlabp(9) mlabgap(*3) mlabcolor(maroon) msize(small) msymbol(i)  mcolor(gs2)), ///
yscale(range (-35 15)) xscale(range (-45 20)) ylabel(-30(10)10,grid) xlabel(-50(10)20,grid) ///
ytitle("%-Change in Cumulative Exports 2020/2019") ///
xtitle("%-Change in Cumulative Imports 2020/2019", margin(small)) ///
yline(0, lcolor(gs10) lwidth(medthick)) xline(0, lcolor(gs10) lwidth(medthick)) ///
scheme(s1color) legend(off)
graph export "${root}\figures\FIG3a_Yexport_Ximport_COUNTRIES_COVID19_Percent.png", replace
graph export "${root}\figures\FIG3a_Yexport_Ximport_COUNTRIES_COVID19_Percent.pdf", replace

